package net.wsfcp.android.ui.view.my.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.wsfcp.android.R;
import net.wsfcp.android.base.BaseActivity;
import net.wsfcp.android.http.mvp.contract.ChangePasswordContract;
import net.wsfcp.android.event.LoginEvent;
import net.wsfcp.android.http.mvp.presenter.ChangePasswordPresenter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

//修改密码
public class ChangePasswordActivity extends BaseActivity<ChangePasswordPresenter> implements ChangePasswordContract.ChangePasswordView {
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_right)
    Button btnRight;

    @BindView(R.id.edt_old)
    EditText edtOld;
    @BindView(R.id.edt_new)
    EditText edtNew;
    @BindView(R.id.edt_repeat)
    EditText edtRepeat;
    @BindView(R.id.txt_save)
    TextView txtSave;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_password;
    }

    @Override
    protected ChangePasswordPresenter loadPresenter() {
        return new ChangePasswordPresenter();
    }

    @Override
    protected void initData() {
        setmTitle(getString(R.string.fragment_my_password));
        setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initListener() {
        txtSave.setOnClickListener(this);
    }

    @Override
    protected void initView() {
        btnLeft = rlLeft;
        mTitle = txtTitle;
        btRight = btnRight;
    }


    @Override
    protected void otherViewClick(View view) {
        switch (view.getId()) {
            case R.id.txt_save:
                mPresenter.changePassword(getOldPwd(), getNewPwd());
                break;

        }
    }

    @Override
    public String getOldPwd() {
        return edtOld.getText().toString().trim();
    }

    @Override
    public String getNewPwd() {
        return edtNew.getText().toString().trim();
    }

    @Override
    public String getRepeatNewPwd() {
        return edtRepeat.getText().toString().trim();
    }

    @Override
    public void changePasswordSuccess() {
        toast("修改密码成功");
        finish();
        EventBus.getDefault().post(new LoginEvent(2));

    }

    @Override
    public void changePasswordFail(String failMsg) {
        toast(failMsg);
    }


    public boolean checkNull() {
        boolean isNull = false;
        if (getOldPwd().length() == 0) {
            toast("旧密码不能为空");
            isNull = true;
        } else if (getNewPwd().length() == 0) {
            toast("新密码不能为空");
            isNull = true;
        } else if (getRepeatNewPwd().length() == 0) {
            toast("确认密码不能为空");
            isNull = true;
        } else if (!getNewPwd().equals(getRepeatNewPwd())) {
            toast("两次输入密码不一致");
            isNull = true;
        } else if (getNewPwd().equals(getOldPwd())) {
            toast("新旧密码不能相同");
            isNull = true;
        }
        return isNull;
    }
}
